////
//
/// \file ./test/block_node_test.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/block_node.hpp>

#include <string>

#include <gtest/gtest.h>

#include <inc/parser/utilities.hpp>

namespace inc {
namespace parser {

using std::string;

TEST(BlockNodeTest, parse_basic1)
{
  const string data[] =
      { "foo()\n", "a := b\n", "foo()\n \n ", "a := (x + \n\n2)\n\n" };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    BlockNode node;
    string::const_iterator last_parsed;
    EXPECT_TRUE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i], string( data[i].begin(), last_parsed ) );
    EXPECT_EQ( data[i], node.get_text() );
    EXPECT_TRUE( node.entries_size() == 1 )
        << "Failed to parse exactly one entry: '" << data[i] << "'";
  }
}

TEST(BlockNodeTest, parse_basic2)
{
  const string data[] =
      { "foo()\nbar()\n", "a := b\nx := y\n",
        "foo()\n \nbar()\n ", "a := (x + \n\n2)\n\nx := y\n  " };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    BlockNode node;
    string::const_iterator last_parsed;
    EXPECT_TRUE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i], string( data[i].begin(), last_parsed ) );
    EXPECT_EQ( data[i], node.get_text() );
    EXPECT_TRUE( node.entries_size() == 2 )
        << "Failed to parse a function name: '" << data[i] << "'";
  }
}

TEST(BlockNodeTest, parse_fail)
{
  const string data[] = { "", "foo()", "\n\nfoo()\n", "( x + y )" };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    BlockNode node;
    string::const_iterator last_parsed = data[i].end();
    EXPECT_FALSE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Erroneously parsed: '" << data[i] << "'";
    EXPECT_EQ( string(), string( data[i].begin(), last_parsed ) );
  }
}

} //  end parser namespace
} //  end inc namespace
